/**
 *Project: jh-framework
 *File: cn.xkshow.framework.web.action.BaseAction.java 
 *Author: hardy(admin@xkshow.cn) Copyright
 ****************************************************************
 *   VERSION       DATE            BY       CHANGE/COMMENT
 * --------------------------------------------------------------
 *    1.0        2013-7-4         hardy         create
 ****************************************************************
 *note:
 ***************************************************************/
package cn.xkshow.framework.web.action;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.xkshow.lang.StringHelper;
import cn.xkshow.plugins.page.PageConstant;
import cn.xkshow.plugins.page.PageList;
import cn.xkshow.plugins.page.Page;
import cn.xkshow.util.ConvertHelper;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class BaseAction extends ActionSupport implements SessionAware,
		ServletRequestAware, ServletResponseAware, ModelDriven<Object> {
	protected final Log logger = LogFactory.getLog(this.getClass());
	protected Map<String, Object> session;
	protected HttpServletRequest request;
	protected HttpServletResponse response;

	protected final String SEARCH = "search";
	protected final String VIEW = "view";
	
	protected String action = "search";
	protected String dataformat = "txt";//Data format: txt|xml|json
	protected String kid;//The primary key id 
	protected String keywords;
	
	protected String executeMethod(String method) throws Exception {
		String result = "";
		try {
			Class[] c = null;
			Method m = this.getClass().getMethod(method, c);
			Object[] o = null;
			result = (String) m.invoke(this, o);
			logger.debug("******baseAction executeMethod result:******"+ result);
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("BaseAction: ERROR:" + e);
		}
		return result;
	}

	protected void copyProperties(Object target, Object o) throws Exception {
		ConvertHelper.copyProperties(target, o);
	}
	
	protected void copyPropertiesWithoutNull(Object target, Object o) throws Exception {
		ConvertHelper.copyPropertiesWithoutNull(target, o);
	}	
	
	protected Object getBean(String name){
	    ServletContext context = ServletActionContext.getServletContext();
	    ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);

	    return ctx.getBean(name);
	}
	
	//page
	private PageList pageList;
	private long dtotal = -1L;
	private int cpage = 1;	
	protected Page getPage(Integer viewLine, String pgurl) {
		Page pagemodel = new Page();
		pagemodel.setCpage(this.cpage>0?this.cpage:1);
		pagemodel.setPerPageNum(null == viewLine ? PageConstant.VIEW_LINE_10: viewLine);	
		if(this.getDtotal() > 0) {
			pagemodel.setDtotal(this.dtotal);
		}
		pagemodel.setPgurl(StringHelper.isNotNull(pgurl) ? pgurl : "");

		return pagemodel;
	}
	
	/**
	 *  implement methods
	 */	
	protected Object oModel;
	public Object getModel() {
		if ( oModel == null ){
			oModel = new Object();
		}
		return oModel;
	}
	
	/* (non-Javadoc)
	 *  getter or setter
	 */	
	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setCpage(int cpage) {
		this.cpage = cpage;
	}

	public long getDtotal() {
		return dtotal;
	}

	public void setDtotal(long dtotal) {
		this.dtotal = dtotal;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public int getCpage() {
		return cpage;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getDataformat() {
		return dataformat;
	}

	public void setDataformat(String dataformat) {
		this.dataformat = dataformat;
	}

	public String getKid() {
		return kid;
	}

	public void setKid(String kid) {
		this.kid = kid;
	}

	public Object getoModel() {
		return oModel;
	}

	public void setoModel(Object oModel) {
		this.oModel = oModel;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

}
